//
//  ViewController.swift
//  ZLAlert
//
//  Created by Zz_l on 11/24/2022.
//  Copyright (c) 2022 Zz_l. All rights reserved.
//

import UIKit
import ZLAlert

class ViewController: UIViewController {
    /// 列表
    @IBOutlet weak var tableview: UITableView!
    let data = [["head": "默认组合",
                 "cell": [
                    ["title": "一个按钮", "action" : "normalOneButton"],
                    ["title": "两个按钮", "action" : "normalDoubleButton"],
                    ["title": "一个输入框", "action" : "normalOneInput"],
                    ["title": "多个输入框", "action" : "normalMoreInput"],
                    ["title": "一个组合", "action" : "normalGroup"]]],
                ["head": "自定义样式",
                 "cell": [
                    ["title": "自定义标题", "action" : "customTitle"],
                    ["title": "自定义内容", "action" : "customContent"],
                    ["title": "自定义输入框", "action" : "customInput"],
                    ["title": "自定义一个按钮", "action" : "customOneButton"],
                    ["title": "自定义两个按钮", "action" : "customDoubleButton"],
                    ["title": "自定义一个组合", "action" : "customGroup"]]],
                ["head": "通过配置自定义",
                 "cell": [
                    ["title": "自定义一个组合", "action" : "customConfigGroup"]]],
                ["head": "简约",
                 "cell": [
                    ["title": "我只想写一行", "action" : "oneLineGroup"]]]]
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}
/// 默认使用
extension ViewController {
    /// 一个按钮
    @objc func normalOneButton() {
        let alert = ZLAlert()
        alert.addTitle("提示")
        alert.addSpace(height: 10)
        alert.addContent("一个按钮的内容,更多内容需要换行更多内容需要换行更多内容需要换行")
        alert.addSpace(height: 5)
        alert.addButton("确定") {
            print("点击确定")
        }
        alert.show()
    }
    /// 两个按钮
    @objc func normalDoubleButton() {
        let alert = ZLAlert()
        alert.addTitle("提示")
        alert.addSpace(height: 10)
        alert.addContent("一个按钮的内容,更多内容需要换行更多内容需要换行更多内容需要换行")
        alert.addSpace(height: 5)
        alert.addDoubleAction(z_left: "确定", {
            print("点击确定")
        }, z_line: true, z_right: "取消") {
            print("点击取消")
        }
        alert.show()
    }
    /// 一个输入框
    @objc func normalOneInput() {
        let alert = ZLAlert()
        alert.addTitle("提示")
        alert.addSpace(height: 10)
        alert.addTextField("说牛批") { text in
            print("你输入了: \(text)")
        }
        alert.addSpace(height: 10)
        alert.addButton("确定") {
            print("点击确定")
        }
        alert.show()
    }
    /// 多个输入框
    @objc func normalMoreInput() {
        let alert = ZLAlert()
        alert.addTitle("提示")
        alert.addSpace(height: 5)
        alert.addTextField("说牛批") { text in
            print("你输入了: \(text)")
        }
        alert.addSpace(height: 5)
        alert.addTextField("说牛批") { text in
            print("你输入了: \(text)")
        }
        alert.addSpace(height: 5)
        alert.addTextField("说牛批") { text in
            print("你输入了: \(text)")
        }
        alert.addSpace(height: 5)
        alert.addTextField("说牛批") { text in
            print("你输入了: \(text)")
        }
        alert.addSpace(height: 5)
        alert.addButton("确定") {
            print("点击确定")
        }
        alert.show()
    }
    @objc func normalGroup() {
        let alert = ZLAlert()
        alert.addTitle("提示")
        alert.addSpace(height: 10)
        alert.addContent("一个按钮的内容,更多内容需要换行更多内容需要换行更多内容需要换行")
        alert.addSpace(height: 10)
        alert.addTextField("说牛批") { text in
            print("你输入了: \(text)")
        }
        alert.addSpace(height: 10)
        alert.addLine()
        alert.addDoubleAction(z_left: "确定", {
            print("点击确定")
        }, z_line: true, z_right: "取消") {
            print("点击取消")
        }
        alert.addLine()
        alert.addDoubleAction(z_left: "再次确定", {
            print("点击再次确定")
        }, z_line: true, z_right: "再次取消") {
            print("点击再次取消")
        }
        alert.addLine()
        alert.addButton("最后的确定") {
            print("点击最后的确定")
        }
        alert.show()
    }
}
/// 自定义方式使用
extension ViewController {
    /// 自定义标题
    @objc func customTitle() {
        for family in UIFont.familyNames {
            print(family)
            for name in UIFont.fontNames(forFamilyName: family) {
                print("-- \(name)")
            }
        }
        let alert = ZLAlert()
        alert.addTitle("自定义标题样式")
        alert.custom { (lastView: UILabel) in
            /// 正常操作它
            lastView.textColor = .orange
            lastView.font = UIFont.boldSystemFont(ofSize: 25)
        }
        alert.addContent("通过方法自定义当前的标题样式")
        alert.addButton("确定") {
            print("点击确定")
        }
        alert.show()
    }
    /// 自定义内容
    @objc func customContent() {
        let alert = ZLAlert()
        alert.addTitle("自定义内容样式")
        alert.addContent("通过方法自定义当前的标题样式")
        alert.custom { (lastView: UILabel) in
            /// 正常操作它
            lastView.textColor = .orange
            lastView.font = UIFont.systemFont(ofSize: 20, weight: .bold)
        }
        alert.addButton("确定") {
            print("点击确定")
        }
        alert.show()
    }
    /// 自定义输入框
    @objc func customInput() {
        let alert = ZLAlert()
        alert.addTitle("自定义输入框样式")
        alert.addTextField("提示语") { text in
            print("你输入的是 == \(text)")
        }
        alert.custom { (lastView: UITextField) in
            /// 正常操作它
            lastView.textColor = .orange
            lastView.font = UIFont.systemFont(ofSize: 20, weight: .bold)
            lastView.clearButtonMode = .always
            let leftIcon = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
            leftIcon.image = UIImage(named: "zl_alert_ok")
            lastView.leftView = leftIcon
            lastView.leftViewMode = .always
            lastView.tintColor = UIColor.green
        }
        alert.addButton("确定") {
            print("点击确定")
        }
        alert.show()
    }
    /// 自定义一个按钮
    @objc func customOneButton() {
        let alert = ZLAlert()
        alert.addTitle("自定义按钮")
        alert.addContent("通过方法自定义当前的按钮样式")
        alert.addButton("确定") {
            print("点击确定")
        }
        alert.custom { (lastView: UIButton) in
            /// 正常操作它
            lastView.setTitleColor(UIColor.orange, for: .normal)
            lastView.setTitleColor(UIColor.red, for: .highlighted)
            lastView.layer.borderWidth = 1
            lastView.layer.borderColor = UIColor.systemPink.cgColor
            lastView.layer.cornerRadius = 5
        }
        alert.show()
    }
    /// 自定义两个按钮
    @objc func customDoubleButton() {
        let alert = ZLAlert()
        alert.addTitle("自定义按钮")
        alert.addContent("通过方法自定义当前的按钮样式")
        alert.addDoubleAction(z_left: "确定", {
            print("点击确定")
        }, z_line: false, z_right: "取消") {
            print("点击取消")
        }
        alert.custom { (lastView: ZLAlertDoubleButton) in
            /// 正常操作它
            /// 左侧按钮
            lastView.leftButton?.setTitleColor(UIColor.orange, for: .normal)
            lastView.leftButton?.setTitleColor(UIColor.red, for: .highlighted)
            lastView.leftButton?.layer.borderWidth = 1
            lastView.leftButton?.layer.borderColor = UIColor.systemPink.cgColor
            lastView.leftButton?.layer.cornerRadius = 5
            lastView.leftButton?.setImage(UIImage(named: "zl_alert_ok"), for: .normal)
            lastView.leftButton?.setImage(UIImage(named: "zl_alert_ok"), for: .highlighted)
            /// 右侧按钮
            lastView.rightButton?.setTitleColor(UIColor.yellow, for: .normal)
            lastView.rightButton?.setTitleColor(UIColor.blue, for: .highlighted)
            lastView.rightButton?.layer.borderWidth = 1
            lastView.rightButton?.layer.borderColor = UIColor.blue.cgColor
            lastView.rightButton?.layer.cornerRadius = 5
            lastView.rightButton?.setImage(UIImage(named: "zl_alert_cancel"), for: .normal)
            lastView.rightButton?.setImage(UIImage(named: "zl_alert_cancel"), for: .highlighted)
        }
        alert.show()
    }
    /// 自定义一个组合
    @objc func customGroup() {
        let alert = ZLAlert()
        alert.addTitle("请输入账号密码")
        alert.addSpace(height: 10)
        alert.addTextField("账号") { text in
            print("你输入的账号是 == \(text)")
        }
        alert.custom { (lastView: UITextField) in
            /// 正常操作它
            lastView.textAlignment = .left
            lastView.clearButtonMode = .always
            let leftTitle = UILabel()
            leftTitle.font = UIFont.systemFont(ofSize: 14)
            leftTitle.text = "  账号："
            lastView.leftView = leftTitle
            lastView.leftViewMode = .always
            lastView.tintColor = UIColor.black
        }
        alert.addSpace(height: 5)
        alert.addTextField("密码") { text in
            print("你输入的密码是 == \(text)")
        }
        alert.custom { (lastView: UITextField) in
            /// 正常操作它
            lastView.textAlignment = .left
            lastView.clearButtonMode = .always
            let leftTitle = UILabel()
            leftTitle.font = UIFont.systemFont(ofSize: 14)
            leftTitle.text = "  密码："
            lastView.leftView = leftTitle
            lastView.leftViewMode = .always
            lastView.tintColor = UIColor.black
            lastView.isSecureTextEntry = true
        }
        alert.addSpace(height: 10)
        alert.addLine()
        alert.addDoubleAction(z_left: "确定", {
            print("点击确定")
        }, z_line: true, z_right: "取消") {
            print("点击取消")
        }
        alert.show()
    }
}

/// 通过配置定义一个组合
extension ViewController {
    @objc func customConfigGroup() {
        /// 标题配置
        var titleConfig = ZLAlertItemConfig()
        titleConfig.color = UIColor.hexColor("#0F1E4C")
        titleConfig.font = UIFont.systemFont(ofSize: 30, weight: .bold)
        titleConfig.alignment = .left
        /// 内容配置
        var contentConfig = ZLAlertItemConfig()
        contentConfig.color = UIColor.white
        contentConfig.font = UIFont.systemFont(ofSize: 20, weight: .bold)
        contentConfig.alignment = .left
        contentConfig.insets = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 10)
        contentConfig.backgroundColor = UIColor.hexColor("#999999", alpha: 0.3)
        contentConfig.borderColor = UIColor.init(white: 0, alpha: 0.3)
        contentConfig.borderWidth = 0.5
        contentConfig.cornerRadius = 5
        /// 输入框配置
        var inputConfig = ZLAlertItemConfig()
        inputConfig.color = UIColor.black
        inputConfig.font = UIFont.systemFont(ofSize: 15)
        inputConfig.alignment = .left
        inputConfig.insets = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 10)
        inputConfig.borderColor = UIColor.init(white: 0, alpha: 0.3)
        inputConfig.borderWidth = 0.5
        inputConfig.cornerRadius = 5
        /// 按钮配置
        var buttonConfig = ZLAlertItemConfig()
        buttonConfig.color = UIColor.black
        buttonConfig.font = UIFont.systemFont(ofSize: 15)
        buttonConfig.alignment = .center
        buttonConfig.insets = UIEdgeInsets.init(top: 0, left: 30, bottom: 0, right: 30)
        buttonConfig.borderColor = UIColor.init(white: 0, alpha: 0.3)
        buttonConfig.borderWidth = 0.5
        buttonConfig.cornerRadius = 5
        buttonConfig.backgroundColor = UIColor.orange
        /// 按钮线
        var lineConfig = ZLAlertItemConfig()
        lineConfig.insets = UIEdgeInsets.init(top: 0, left: 30, bottom: 0, right: 30)
        lineConfig.backgroundColor = UIColor.red
        lineConfig.height = 0.5
        let alert = ZLAlert()
        alert.addTitle("自定义配置", titleConfig)
        alert.addSpace(height: 10)
        alert.addContent("自定义配置显示内容自定义配置显示内容自定义配置显示内容", contentConfig)
        alert.addSpace(height: 10)
        alert.addTextField("", "说牛批", inputConfig) { text in
            print("正在编辑 == \(text)")
        } editEnd: { text in
            print("编辑完成 == \(text)")
        }
        alert.addSpace(height: 10)
        alert.addButton("确定", true, buttonConfig) {
            print("点击配置定义一个组合 确定")
        }
        alert.addSpace(height: 10)
        alert.addDoubleAction(z_left: "左侧", true, buttonConfig, {
            print("点击配置定义一个组合 左侧")
        }, z_line: false, lineConfig, z_right: "右侧", true, buttonConfig) {
            print("点击配置定义一个组合 右侧")
        }
        alert.addSpace(height: 5)
        alert.show()
    }
}

/// 简便写
extension ViewController {
    @objc func oneLineGroup() {
        ZLAlert()
            .addTitle("提示")
            .addSpace(height: 10)
            .addContent("一个按钮的内容,更多内容需要换行更多内容需要换行更多内容需要换行")
            .addSpace(height: 5)
            .addTextField("说牛批") { text in
                print("你输入了: \(text)")
            }.addSpace(height: 10)
            .addLine()
            .addButton("最后的确定") {
                print("点击最后的确定")
            }
            .show()
    }
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        let section = data[section]
        return (section["head"] as! String)
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let section = data[indexPath.section]
        let cells = section["cell"] as! Array<Any>
        let dict = cells[indexPath.row] as! Dictionary<String, String>
        let cell = UITableViewCell()
        cell.textLabel!.text = dict["title"]
        return cell
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return data.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let section = data[section]
        let cells = section["cell"] as! Array<Any>
        return cells.count
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let section = data[indexPath.section]
        let cells = section["cell"] as! Array<Any>
        let dict = cells[indexPath.row] as! Dictionary<String, String>
        let sel = Selector(dict["action"]!)
        self.perform(sel)
    }
}

extension UIColor {
    class func hexColor(_ hexString: String, alpha: CGFloat = 1.0) -> UIColor {
        var hex = hexString.hasPrefix("#") ? String(hexString.dropFirst()) : hexString
        guard hex.count == 3 || hex.count == 6  else {
            return UIColor.clear
        }
        
        if hex.count == 3 {
            for (indec, char) in hex.enumerated() {
                hex.insert(char, at: hex.index(hex.startIndex, offsetBy: indec * 2))
            }
        }
        
        let color = UIColor.init(
            red: CGFloat((Int(hex, radix: 16)! >> 16) & 0xFF) / 255.0,
            green: CGFloat((Int(hex, radix: 16)! >> 8) & 0xFF) / 255.0,
            blue: CGFloat((Int(hex, radix: 16)!) & 0xFF) / 255.0,
            alpha: alpha
        )
        return color
    }
}
